package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Like;
import com.example.mapper.LikeMapper;
import com.example.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.stream.Collectors;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;

    @Override
    public void likeProduct(Long userId, Long productId) {
        if (!hasLiked(userId, productId)) {
            Like like = new Like();
            like.setUserId(userId);
            like.setProductId(productId);
            likeMapper.insert(like);
        }
    }

    @Override
    public void unlikeProduct(Long userId, Long productId) {
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("`user_id`", userId)
               .eq("`product_id`", productId);
        likeMapper.delete(wrapper);
    }

    @Override
    public boolean hasLiked(Long userId, Long productId) {
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("`user_id`", userId)
               .eq("`product_id`", productId);
        return likeMapper.selectCount(wrapper) > 0;
    }

    @Override
    public Set<Long> getUserLikedProductIds(Long userId) {
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return likeMapper.selectList(wrapper).stream()
                .map(Like::getProductId)
                .collect(Collectors.toSet());
    }
} 